package com.glsc.ngateway.common.base.repo.mysql.gateway.platform;


import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.flow.FlowConfig;
import com.glsc.ngateway.common.api.flowable.dto.task.config.FlowConfigSearchDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotBlank;
import java.util.List;

@Repository
public interface FlowConfigRepo extends JpaRepository<FlowConfig, Long>, JpaSpecificationExecutor<FlowConfig> {

    @Query(value =
            "select * from t_gw_it_flow_config h " +
                    "where h.deleted = 0 " +
                    "and (:#{#param.processDefKey} is null or h.process_def_key = :#{#param.processDefKey}) " +
                    "and (:#{#param.taskDefKey} is null or h.task_def_key = :#{#param.taskDefKey}) " +
                    "and (:#{#param.configType} is null or h.config_type = :#{#param.configType}) " +
                    "and (:#{#param.role} is null or (h.value_type = 'ROLE' and FIND_IN_SET(:#{#param.role}, h.value) > 0)) " +
                    "and (:#{#param.account} is null or (h.value_type = 'ACCOUNT' and FIND_IN_SET(:#{#param.account}, h.value) > 0)) " +
                    "order by h.process_def_key asc " +
                    ""
            , countQuery =
            "select count(*) from t_gw_it_flow_config h " +
                    "where h.deleted = 0 " +
                    "and (:#{#param.processDefKey} is null or h.process_def_key = :#{#param.processDefKey}) " +
                    "and (:#{#param.taskDefKey} is null or h.task_def_key = :#{#param.taskDefKey}) " +
                    "and (:#{#param.configType} is null or h.config_type = :#{#param.configType}) " +
                    "and (:#{#param.role} is null or (h.value_type = 'ROLE' and FIND_IN_SET(:#{#param.role}, h.value) > 0)) " +
                    "and (:#{#param.account} is null or (h.value_type = 'ACCOUNT' and FIND_IN_SET(:#{#param.account}, h.value) > 0)) " +
                    ""
            , nativeQuery = true
    )
    Page<FlowConfig> pageParam(Pageable pageable, FlowConfigSearchDto param);

    @Query(value =
            "select * from t_gw_it_flow_config h " +
                    "where h.deleted = 0 " +
                    "and (:#{#param.processDefKey} is null or h.process_def_key = :#{#param.processDefKey}) " +
                    "and (:#{#param.taskDefKey} is null or h.task_def_key = :#{#param.taskDefKey}) " +
                    "and (:#{#param.configType} is null or h.config_type = :#{#param.configType}) " +
                    "and (:#{#param.role} is null or (h.value_type = 'ROLE' and FIND_IN_SET(:#{#param.role}, h.value) > 0)) " +
                    "and (:#{#param.account} is null or (h.value_type = 'ACCOUNT' and FIND_IN_SET(:#{#param.account}, h.value) > 0)) " +
                    "order by h.process_def_key asc " +
                    ""
            , nativeQuery = true)
    List<FlowConfig> query(@Param("param") FlowConfigSearchDto param);

    @Transactional
    @Modifying
    void deleteByProcessDefKeyIsAndTaskDefKeyIs(@NotBlank String processKey, @NotBlank String taskKey);

}
